package com.mytest;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.ResultSetInternalMethods;
import java.security.MessageDigest;

@SuppressWarnings("serial")
public class Connect extends HttpServlet{
		Connection conn = null;
		String sql,sql_info;
		String resultString ;
		String url_read = "jdbc:mysql://db4free.net:3306";
		String username_db = "tsbx";
		String password_db = "mengwei";
		Statement  stmt;
		int result = 0,result_1 = 0;
		
		/*String url_read = "jdbc:mysql://r.rdc.sae.sona.con.cn:3307/app_tsbx";
		String url_write = "jdbc:mysql://w.rdc.sae.sona.con.cn:3307/app_tsbx";
		String username_db="wzy3mxx4yk";
		String password_db="jxh4li5xm3y4h44wmmi1y4k3x122mlwz540z04yw";
		*/
		
		public String md5string(String preString){
		
			String result = new String();
			try{
				
				MessageDigest mdInst = MessageDigest.getInstance("MD5");
				mdInst.update(preString.getBytes());
				byte[] md = mdInst.digest();
				StringBuffer buf = new StringBuffer("");
				int k ;
				for(int i=0;i<md.length;i++){
					k=md[i];
					byte byte0 = md[i];
					if(k<0)
						i+=256;
					if(k<16)
						buf.append("0");
					buf.append(Integer.toHexString(k));
				}
				result = buf.toString();
			}catch (Exception e){
				
			}
			return result;
		}
		public String encryption(String plainText) {
			String re_md5 = new String();
			try {
				MessageDigest md = MessageDigest.getInstance("MD5");
				md.update(plainText.getBytes());
				byte b[] = md.digest();

				int i;

				StringBuffer buf = new StringBuffer("");
				for (int offset = 0; offset < b.length; offset++) {
					i = b[offset];
					if (i < 0)
						i += 256;
					if (i < 16)
						buf.append("0");
					buf.append(Integer.toHexString(i));
				}

				re_md5 = buf.toString();

			} catch (Exception e) {
				e.printStackTrace();
			}
			return re_md5;
		}

		public String get_username(String username){
			try{
				connect();
				sql = "select username from login  where username='"+username+"'";
				//sql = "select password from login where username='"+username+"'";;
				if(-1 == -1){
					System.out.println("success select");
					ResultSet rs = (ResultSet) stmt.executeQuery(sql);
					while(rs.next()){
						resultString = rs.getString(1);
						System.out.println(rs.getString(1));
					}
					
				}
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			return resultString;
		}
		public String get_usercity(String username){
			try{
				
				connect();
				sql = "select usercity from userinfo where username='"+username+"'";
				//sql = "select * from login";
				if(-1 == -1){
					System.out.println("success select");
					ResultSet rs = (ResultSet) stmt.executeQuery(sql);
					while(rs.next()){
						resultString = rs.getString(1);
						System.out.println(rs.getString(1));
					}
					
				}
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			
			System.out.println(resultString);
			return resultString;
		}

		public String  get_password(String username){
			try{
			
				connect();
				sql = "select password from login where username='"+username+"'";
				//sql = "select * from login";
				if(-1 == -1){
					System.out.println("success select");
					ResultSet rs = (ResultSet) stmt.executeQuery(sql);
					while(rs.next()){
						resultString = rs.getString(1);
						System.out.println(rs.getString(1));
					}
					
				}
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			resultString = encryption(resultString);
			System.out.println(resultString);
			return resultString;
		}
		
		public boolean get_userregister(String username,String password,String usercity){
			try{
				
				connect();
				sql = "insert into login values('"+username+"','"+password+"')";
				sql_info =  "insert into userinfo values('"+username+"','"+usercity+"')";
				//sql = "select * from login";
				result = stmt.executeUpdate(sql);
				result_1 = stmt.executeUpdate(sql_info);
				System.out.println(result+result_1);
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			if(result == 1 && result_1 == 1)
				return true;
			return false;
		}
		public void connect(){
			try{
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				conn = (Connection) DriverManager.getConnection(url_read,username_db,password_db);
				stmt = (Statement) conn.createStatement();
				sql = "use tsbx";
				stmt.executeQuery(sql);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
}
